﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.ComponentModel;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Commands;
using EventInfrastructure;
using ModuleProductEdit.Views;
using ModulesInfrastructure.ViewModels;
using ServerConnection.ShopServiceReference;
using ServerConnection;
using ShellInfrastructure;
using ModuleProductEdit.HelpClass;

namespace ModuleProductEdit.ViewModels
{
    public class ProductEditViewModel : SharedViewModel, IProductEditViewModel
    {

        #region private fild

        private bool m_flagAdd;
        private IEventAggregator m_eventAggregator;
        private ICommand m_OkProductCommand;
        private ICommand m_CancelProductCommand;
        private ICommand m_AddNewImageCommand;
        private ICommand m_AddLocalizationCommand;
        private ICommand m_DeleteLocalizationCommand;
        private ICommand m_DeleteImageCommand;

        private ProductLocalizationModel m_selectedLocalization;
        private GalleryClass m_selectedGallery;
        private CategoryView m_selectedCategory;
        private CategoryView m_selectedSubCategory;

        #endregion

        #region public properties

        [Dependency]
        public IRegionManager RegionManager { get; set; }

        public List<GalleryClass> LoadGallery { get; private set; }
        public ProductAll ShowProduct { get; private set; }
        public List<CategoryView> LoadCategory { get; private set; }
        public List<CategoryView> LoadSubCategory { get; private set; }
        public decimal productPrice { get; set; }

        public ICommand DeleteImageCommand
        {
            get { return m_DeleteImageCommand; }
            set
            {
                m_DeleteImageCommand = value;
                RaisePropertyChanged(() => DeleteImageCommand);

            }
        }

        public ICommand AddNewImageCommand
        {
            get { return m_AddNewImageCommand; }
            set
            {
                m_AddNewImageCommand = value;
                RaisePropertyChanged(() => AddNewImageCommand);
                   
            }
        }

        public ICommand AddLocalizationCommand
        {
            get { return m_AddLocalizationCommand; }
            set
            {
                m_AddLocalizationCommand = value;
                RaisePropertyChanged(() => AddLocalizationCommand);

            }
        }

        public ICommand DeleteLocalizationCommand
        {
            get { return m_DeleteLocalizationCommand; }
            set
            {
                m_DeleteLocalizationCommand = value;
                RaisePropertyChanged(() => DeleteLocalizationCommand);

            }
        }

        public ICommand CancelProductCommand
        {
            get { return m_CancelProductCommand; }
            set
            {
                m_CancelProductCommand = value;
                RaisePropertyChanged(() => CancelProductCommand);
            }
        }


        public ICommand OkProductCommand
        {
            get { return m_OkProductCommand; }
            set
            {
                m_OkProductCommand = value;
                RaisePropertyChanged(() => OkProductCommand);
            }
        }


        public CategoryView OnSelectedCategory
        {
            get { return m_selectedCategory; }
            set
            {
                m_selectedCategory = value;
                if (m_selectedCategory != null)
                {
                    this.LoadSubCategory = Service.Instance.GetCategories("eng", m_selectedCategory.Id).ToList();
                    RaisePropertyChanged(() => this.LoadSubCategory);
                }
            }
        }

        public CategoryView OnSelectedSubCategory
        {
            get { return m_selectedSubCategory; }
            set
            {
                m_selectedSubCategory = value;
                RaisePropertyChanged(() => this.m_selectedSubCategory);
                RaisePropertyChanged(() => this.OnSelectedSubCategory);
            }
        }

        public GalleryClass SelectedGallery
        {
            get { return m_selectedGallery; }
            set
            {
                m_selectedGallery = value;
                RaisePropertyChanged(() => this.m_selectedGallery);
                RaisePropertyChanged(() => this.SelectedGallery);
            }

        }

        public ProductLocalizationModel SelectedLocalization
        {
            get { return m_selectedLocalization; }
            set
            {
                m_selectedLocalization = value;
                RaisePropertyChanged(() => this.m_selectedLocalization);
                RaisePropertyChanged(() => this.SelectedLocalization);
            }

        }

        #endregion

        #region constructor

        public ProductEditViewModel(IEventAggregator eventAggregator)
        {
            this.m_eventAggregator =eventAggregator;
            if (Service.Instance.GetCategories("eng", 0)!=null)
            this.LoadCategory = Service.Instance.GetCategories("eng", 0).ToList();
            m_OkProductCommand = new DelegateCommand(onOkProductCommand);
            m_CancelProductCommand = new DelegateCommand(onCancelProductCommand);
            m_AddNewImageCommand = new DelegateCommand(onAddNewImageCommand);
            m_AddLocalizationCommand = new DelegateCommand(onAddLocalizationCommand);
            m_DeleteLocalizationCommand = new DelegateCommand(onDeleteLocalizationCommand);
            m_DeleteImageCommand = new DelegateCommand(onDeleteImageCommand);
            eventAggregator.GetEvent<ProductEditEvent>().Subscribe(onProductEditEvent);
            eventAggregator.GetEvent<ProductAddEvent>().Subscribe(onProductAddEvent);
        }

        #endregion

        #region private function

        private void onProductEditEvent(long id)
        {
            m_flagAdd = false;
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.RightRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<IProductEditView>());
            ShowProduct=Service.Instance.GetProductAllById(id);
            RaisePropertyChanged(() => this.ShowProduct);

            LoadGallery = new List<GalleryClass>();
            foreach (string item in ShowProduct.Gallery)
            {
                LoadGallery.Add(new GalleryClass() { ProductImage = item });
            }
            RaisePropertyChanged(() => this.LoadGallery);

            int parentId = (int)Service.Instance.GetCtegoryById(ShowProduct.CategoryId, "eng").ParentId;
            m_selectedCategory = (from p in LoadCategory where p.Id.Equals(parentId) select p).First();
            OnSelectedCategory = m_selectedCategory;
            RaisePropertyChanged(() => this.OnSelectedCategory);

            m_selectedSubCategory = (from p in LoadSubCategory where p.Id.Equals(ShowProduct.CategoryId) select p).First();
            OnSelectedSubCategory = m_selectedSubCategory;
            RaisePropertyChanged(() => this.OnSelectedSubCategory);  
        }

        private void onProductAddEvent(bool b)
        {
            m_flagAdd = true;
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.RightRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<IProductEditView>());
            ShowProduct=new ProductAll();
            RaisePropertyChanged(() => this.ShowProduct);
           // ProductLocalizationModel m_selectedLocalization = null;
            RaisePropertyChanged(() => this.m_selectedLocalization);
            LoadGallery = new List<GalleryClass>();
            RaisePropertyChanged(() => this.LoadGallery);
            m_selectedGallery = null;
            SelectedGallery = m_selectedGallery;
            RaisePropertyChanged(() => this.SelectedGallery);
            m_selectedCategory = null;
            OnSelectedCategory = m_selectedCategory;
            RaisePropertyChanged(() => this.OnSelectedCategory);
            m_selectedSubCategory = null;
            OnSelectedSubCategory = m_selectedSubCategory;
            RaisePropertyChanged(() => this.OnSelectedSubCategory);  
            LoadSubCategory = null;
            RaisePropertyChanged(() => this.LoadSubCategory);
        }

        private void onOkProductCommand()
        { 
            ShowProduct.Gallery = new string[LoadGallery.Count()];
            int i=0;
            foreach (GalleryClass item in LoadGallery)
            {
                ShowProduct.Gallery[i] = item.ProductImage;
                i++;
            }
            ShowProduct.CategoryId = m_selectedSubCategory.Id;

            if (m_flagAdd.Equals(true))
            {
                Service.Instance.AddProduct(ShowProduct);
                m_flagAdd = false;
            }
            else
            {
                ProductAll pr = Service.Instance.UpdateProduct(ShowProduct);
            }

            m_eventAggregator.GetEvent<ProductSaveEvent>().Publish(true);   
        }

        private void onCancelProductCommand()
        {
            if(ShowProduct != null && ShowProduct.Id != 0)
            m_eventAggregator.GetEvent<ProductSelectEvent>().Publish(ShowProduct.Id);
            else
                m_eventAggregator.GetEvent<SelectCategoryEvent>().Publish(true);
        }



        private void onAddNewImageCommand()
        {
           List<GalleryClass> list = new List<GalleryClass>();
           foreach (GalleryClass item in LoadGallery)
           {
                list.Add(item);
           }
           LoadGallery = new List<GalleryClass>();
           list.Add(new GalleryClass() { ProductImage = (LoadGallery.Count + 1).ToString() });
           LoadGallery = list;
           RaisePropertyChanged(() => this.LoadGallery);
        }

        private void onAddLocalizationCommand()
        {
            if (ShowProduct.Localization != null)
            {
                ProductLocalizationModel[] pArray = ShowProduct.Localization;
                this.ShowProduct.Localization = new ProductLocalizationModel[pArray.Length + 1];
                for (int i = 0; i < pArray.Length; i++)
                {
                    this.ShowProduct.Localization[i] = pArray[i];
                }
                this.ShowProduct.Localization[pArray.Length] = new ProductLocalizationModel();
            }
            else
            {
                this.ShowProduct.Localization = new ProductLocalizationModel[1];
                this.ShowProduct.Localization[0] = new ProductLocalizationModel();
            }
            RaisePropertyChanged(() => this.ShowProduct.Localization);
            RaisePropertyChanged(() => this.ShowProduct);      
        }

        private void onDeleteLocalizationCommand()
        {
            ProductLocalizationModel[] pArray = ShowProduct.Localization;
            this.ShowProduct.Localization = new ProductLocalizationModel[pArray.Length - 1];
            for (int i = 0; i < this.ShowProduct.Localization.Length; i++)
            {
                if (!pArray[i].Equals(m_selectedLocalization))
                this.ShowProduct.Localization[i] = pArray[i];
            }
            RaisePropertyChanged(() => this.ShowProduct);
        }

        private void onDeleteImageCommand()
        {
            List<GalleryClass> list = new List<GalleryClass>();
            foreach (GalleryClass item in LoadGallery)
            {
                if(!item.Equals(m_selectedGallery))
                list.Add(item);
            }
            LoadGallery = new List<GalleryClass>();
            LoadGallery = list;
            RaisePropertyChanged(() => this.LoadGallery);
        }

        #endregion

    }
}
